package com.cw.subject.domain.handler.subject;

import com.cw.subject.common.enums.IsDeletedFlagEnum;
import com.cw.subject.common.enums.SubjectInfoTypeEnum;
import com.cw.subject.domain.convert.JudgeSubjectConverter;
import com.cw.subject.domain.convert.MultipleSubjectConverter;
import com.cw.subject.domain.entity.SubjectAnswerBO;
import com.cw.subject.domain.entity.SubjectInfoBO;
import com.cw.subject.domain.entity.SubjectOptionBO;
import com.cw.subject.infra.basic.entity.SubjectJudgePO;
import com.cw.subject.infra.basic.entity.SubjectMultiplePO;
import com.cw.subject.infra.basic.service.SubjectMultiplePOService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 多选题目的策略类
 * 
 * @author: 江传文
 * @date: 2024/4/12 16:50
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectMultiplePOService subjectMultiplePOService;
    
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE; // 只处理MULTIPLE类型的
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //多选题目的插入
        List<SubjectMultiplePO> subjectMultipleList = new LinkedList<>();
        if (subjectInfoBO != null && subjectInfoBO.getOptionList() != null){
            subjectInfoBO.getOptionList().forEach(option -> {
                    SubjectMultiplePO subjectMultiplePO = MultipleSubjectConverter.INSTANCE.convertBoToEntity(option);
                    subjectMultiplePO.setSubjectId(subjectInfoBO.getId());
                    subjectMultiplePO.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
                    subjectMultipleList.add(subjectMultiplePO);
            });
            subjectMultiplePOService.batchInsert(subjectMultipleList);
        }
    }

    @Override
    public SubjectOptionBO query(int subjectId) {
        SubjectMultiplePO subjectMultiplePO = new SubjectMultiplePO();
        subjectMultiplePO.setSubjectId(subjectId);
        List<SubjectMultiplePO> result = subjectMultiplePOService.queryByCondition(subjectMultiplePO);
        List<SubjectAnswerBO> subjectAnswerBOList = MultipleSubjectConverter.INSTANCE.convertEntityToBoList(result);

        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }
}
